package com.gemme.gemme.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.gemme.gemme.R;

public class SwitchView extends RelativeLayout {
    private Context context;
    private OnSwitchChange onSwitchChange;

    public SwitchView(Context context) {
        this(context,null);
    }

    public SwitchView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SwitchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }

    RelativeLayout rl_switch;
    ImageView iv_on;
    ImageView iv_off;
    boolean isOn = false;
    private void initView() {
        LayoutInflater.from(context).inflate(R.layout.switch_view,this);

        rl_switch = findViewById(R.id.rl_switch);
        iv_on = findViewById(R.id.iv_on);
        iv_off = findViewById(R.id.iv_off);

        setSatate();
        rl_switch.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                isOn = !isOn;
                setSatate();

                if(onSwitchChange != null)
                    onSwitchChange.OnSwitchChange(isOn);

            }
        });
    }

    private void setSatate() {
        if(isOn){
            iv_on.setVisibility(VISIBLE);
            iv_off.setVisibility(GONE);

        }else{
            iv_on.setVisibility(GONE);
            iv_off.setVisibility(VISIBLE);
        }
    }

    public Boolean getState(){
        return isOn;
    }


    public void setState(Boolean isOn) {
        this.isOn = isOn;
        setSatate();
    }

    public void setOnSwitchChange(OnSwitchChange onSwitchChange){

        this.onSwitchChange = onSwitchChange;
    }

    public interface OnSwitchChange{
        void OnSwitchChange(boolean isOn);
    }

    public void noCan(){
        rl_switch.setOnClickListener(null);
    }
}
